package com.dearbinge.utils.httpClient;



import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ApiRequest {

	public enum Verb {
		GET, POST, PUT, DELETE, PATCH
	}

	private final String url;
	private final Verb verb;

	private String charset;

	private Map<String, String> headers = new HashMap();
	private Map<String, String> params = new HashMap();
	// for post
	private HttpEntity entity;
	private ContentType contentType = null;

	/**
	 * @param url
	 * @param verb
	 */
	public ApiRequest(String url, Verb verb) {
		this.url = url;
		this.verb = verb;
	}

	/**
	 * @param headerName
	 * @param headerVal
	 * @return
	 */
	public ApiRequest header(String headerName, String headerVal) {
		Requires.hasText(headerName, "headerName must not be null or empty.");
		this.headers.put(headerName, headerVal);
		return this;
	}

	/**
	 * @param headers
	 * @return
	 */
	public ApiRequest headers(Map<String, String> headers) {
		Requires.notEmpty(headers, "headers must not be null or empty.");
		this.headers.putAll(headers);
		return this;
	}

	/**
	 * @param paramName
	 * @param paramVal
	 * @return
	 */
	public ApiRequest params(String paramName, String paramVal) {
		Requires.hasText(paramName, "paramName must not be null or empty.");
		this.params.put(paramName, paramVal);
		return this;
	}

	/**
	 * @param entity
	 * @return
	 */
	public ApiRequest entity(HttpEntity entity) {
		Requires.isTrue(verb != Verb.GET, "Get verb cann't accept entity.");
		Requires.notNull(entity, "entity must not be null.");
		this.entity = entity;
		return this;
	}

	public ApiRequest setParams(Map<String, String> params) {
		this.params = params;
		return this;
	}


	public ApiRequest setHeaders(Map<String, String> headers) {
		this.headers = headers;
		return this;
	}

	public ApiRequest setEntity(HttpEntity entity) {
		this.entity = entity;
		return this;
	}

	public ApiRequest setContentType(ContentType contentType) {
		this.contentType = contentType;
		return this;
	}

	public ApiRequest charset(String charset) {
		this.charset = charset;
		return this;
	}

	public String getUrl() {
		return url;
	}

	public Verb getVerb() {
		return verb;
	}

	public Map<String, String> getHeaders() {
		return Collections.unmodifiableMap(headers);
	}

	public HttpEntity getEntity() {
		return entity;
	}

	public Map<String, String> getParams() {
		return params;
	}

	public String getCharset() {
		return charset;
	}

	public ContentType getContentType() {
		return contentType;
	}

	public ApiResponse send() {
		return RequestSinker.sink(this);
	}

}
